package hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.StringUtils;
import hospital.model.hosp.Department;
import hospital.repository.DepartmentRepository;
import hospital.service.DepartmentService;
import hospital.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @description:
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * @Author yang
     * @Description //TODO //保存或修改科室的数据
     * @Param [parameterMap]
     * @return
     **/
    @Override
    public void saveOrUpdate(Map<String, Object> parameterMap) {
        //先通过JSONObject 转化成字符串 -> Hospital对象
        String str = JSONObject.toJSONString(parameterMap);
        Department department = JSONObject.parseObject(str, Department.class);

        //判断该对象是否存在
        Department departmentExit=departmentRepository
                .getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        //对象存在，则为修改
        if(null!=departmentExit){
            department.setId(departmentExit.getId());
            department.setCreateTime(departmentExit.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(department.getIsDeleted());
            //保存科室信息
            departmentRepository.save(department);
        }else{ //则为添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            //保存科室信息
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department>  findDepartment(Map<String, Object> parameterMap) {
        String hoscode = (String) parameterMap.get("hoscode");
        String limitStr= (String) parameterMap.get("limit");
        Integer limit=StringUtils.isNullOrEmpty(limitStr)?1:Integer.parseInt(limitStr);
        String pageStr= (String) parameterMap.get("page");
        Integer page=StringUtils.isNullOrEmpty(pageStr)?1:Integer.parseInt(pageStr);
        //创建分页对象 第一页为0
        PageRequest pageRequest=PageRequest.of(page-1,limit);
        Department department=new Department();
        department.setHoscode(hoscode);
        //设置模糊查询规则
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> departmentExample=Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(departmentExample, pageRequest);
        return all;
    }

    /**
     * @Author yang
     * @Description //TODO //根据hoscode descode删除科室信息
     * @Param [parameterMap]
     * @return
     **/
    @Override
    public void deleteDepartment(Map<String, Object> parameterMap) {
        String hoscode = (String) parameterMap.get("hoscode");
        String depcode = (String) parameterMap.get("depcode");
        //获取科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(null!=department){
            //删除科室信息
            departmentRepository.delete(department);
        }
    }

    /**
     * @Author yang
     * @Description //TODO 根据医院的编号，得到所有的科室列表
     * @Param [hoscode]
     * @return
     **/
    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        //构建查询条件
        Department department=new Department();
        department.setHoscode(hoscode);
        Example<Department> example=Example.of(department);
        //根据hoscode 查询出所有的科室数据
        List<Department> departmentList = departmentRepository.findAll(example);

        //通过java8中流的方式，将departmentList的数据，根据bigcode进行分组
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //将departmentList中的Department,转化为DepartmentVo
        List<DepartmentVo> reslut=new ArrayList<>();

        //遍历map集合 key为bigcode value为bigcode相同的子科室
        for(String bigcode: departmentMap.keySet()){
            DepartmentVo departmentVo = new DepartmentVo();
            //设置科室编号
            departmentVo.setDepcode(bigcode);
            //设置科室名称
            departmentVo.setDepname(departmentMap.get(bigcode).get(0).getBigname());
            //子科室集合
            List<DepartmentVo> childList=new ArrayList<>();
            for(Department dep:departmentMap.get(bigcode)){
                DepartmentVo child = new DepartmentVo();
                //设置科室编号
                child.setDepcode(dep.getDepcode());
                //设置科室名称
                child.setDepname(dep.getDepname());
                childList.add(child);
            }
            //设置子科室集合
            departmentVo.setChildren(childList);
            reslut.add(departmentVo);
        }
        return reslut;
    }

    /**
     * @Author yang
     * @Description //TODO //通过hoscode depcode 获取科室名称
     * @Param [hoscode, depcode]
     * @return
     **/
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        return department !=null?department.getDepname():null;
    }

    /**
     * @Author yang
     * @Description //TODO 通过hoscode depcode 获取科室名称
     * @Param [hoscode, depcode]
     * @return
     **/
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }
}
